/*
 * TopoGraphPanel.java
 *
 * Created on August 30, 2007, 9:26 PM
 *
 * Copyright 2008 David D. Emory
 * 
 */

package org.fpdev.apps.admin.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.fpdev.core.basenet.Path;
import org.fpdev.apps.admin.ACEvent;
import org.fpdev.apps.admin.AdminClient;
import org.fpdev.apps.admin.EventTypes;
import org.fpdev.apps.admin.gui.map.HighlightedPoint;
import org.fpdev.apps.admin.gui.map.MapDrawItems;

/**
 *
 * @author demory
 */
public class TopoGraphPanel extends JPanel {
  
  private AdminClient av_;
  private JScrollPane scrollPane_;
  private MarkerImagePanel imgPanel_;
  private HighlightedPoint point_;
  private Path tripPath_;
  private int leftMargin_, imgHeight_;
  private double tripWidthPx_;

  private Timer leftTimer_, rightTimer_;
  
  public TopoGraphPanel(BufferedImage img, int leftMargin, int imgHeight, AdminClient av) {
    super(new GridLayout(1,1));
    
    av_ = av;
    leftMargin_ = leftMargin;
    imgHeight_ = imgHeight;
    tripPath_ = av_.getAnalysisOps().getActiveBikeTripItem().getPath(av_.getEngine());

    
    imgPanel_ = new MarkerImagePanel(img);
    scrollPane_ = new JScrollPane(imgPanel_);
    tripWidthPx_ = imgPanel_.getWidth() - leftMargin_;
    point_ = new HighlightedPoint(0, 0, Color.BLUE, 5);

    imgPanel_.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        double xAdj = e.getX() - leftMargin_;
        double tripPosition = xAdj/tripWidthPx_;
        if(tripPosition >= 0 && tripPosition <= 1) {
          Point2D.Double pt = tripPath_.getPoint(tripPosition)[0];

          ACEvent evt = new ACEvent(EventTypes.MCLICK_RECENTER);
          evt.setProperty("worldX", pt.x);
          evt.setProperty("worldY", pt.y);
          av_.fireEvent(evt);
        }
      }

      @Override
      public void mouseEntered(MouseEvent e) {
        av_.getGUI().getMapPanel().getDrawItems().addItem(MapDrawItems.AN_CURRENT_LOCATION, point_, "a", 10);
        //System.out.println("showing");
      }

      @Override
      public void mouseExited(MouseEvent e) {
        av_.getGUI().getMapPanel().getDrawItems().removeItem(MapDrawItems.AN_CURRENT_LOCATION);
        av_.getGUI().getMapPanel().refresh(false, false, true);
        imgPanel_.setX(0);
        repaint();

        cancelScrollTimers();
        //System.out.println("hiding");\
      }      
    });
    
    imgPanel_.addMouseMotionListener(new MouseMotionAdapter() {

      @Override
      public void mouseMoved(MouseEvent e) {
        int panelX = e.getXOnScreen()-TopoGraphPanel.this.getLocationOnScreen().x;
        if(panelX < 20) {
          initLeftTimer(20-panelX);
        }
        else if(TopoGraphPanel.this.getWidth()-panelX < 20) {
          initRightTimer(20-(TopoGraphPanel.this.getWidth()-panelX));
        }
        else cancelScrollTimers();


        double xAdj = e.getX() - leftMargin_;
        double tripPosition = xAdj/tripWidthPx_;
        if(tripPosition >= 0 && tripPosition <= 1) {
          Point2D.Double pt = tripPath_.getPoint(tripPosition)[0];
          point_.setXY(pt.getX(), pt.getY());
          av_.getGUI().getMapPanel().refresh(false, false, true);

          imgPanel_.setX(e.getX());
          imgPanel_.repaint();
        }
      }
    }); 


    // set up components

    add(scrollPane_);
    
  }

  private void initLeftTimer(final int delta) {
    if(leftTimer_ != null) leftTimer_.cancel();
    
    leftTimer_ = new Timer();

    leftTimer_.scheduleAtFixedRate(new TimerTask() {

      public void run() {
        int val = scrollPane_.getHorizontalScrollBar().getValue() - delta;
        if(val < 0) val = 0;
        scrollPane_.getHorizontalScrollBar().setValue(val);
      }
    }, 0, 100);

    imgPanel_.setX(0);
  }

  private void initRightTimer(final int delta) {
    if(rightTimer_ != null) rightTimer_.cancel();

    rightTimer_ = new Timer();

    rightTimer_.scheduleAtFixedRate(new TimerTask() {

      public void run() {
        int val = scrollPane_.getHorizontalScrollBar().getValue() + delta;
        if(val < 0) val = 0;
        scrollPane_.getHorizontalScrollBar().setValue(val);
      }
    }, 0, 100);

    imgPanel_.setX(0);

  }

  private void cancelScrollTimers() {
    if(leftTimer_ != null) {
      leftTimer_.cancel();
      leftTimer_ = null;
    }
    if(rightTimer_ != null) {
      rightTimer_.cancel();
      rightTimer_ = null;
    }
  }

  private class MarkerImagePanel extends ImagePanel {
    
    int mx_;
    
    public MarkerImagePanel(BufferedImage img) {
      super(img);
      mx_ = 0;
    }
    
    @Override
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      
      if(mx_ > 0) {
        g.setColor(Color.blue);
        g.drawRect(mx_, 25, 0, imgHeight_ - 26);
      }      
    }
    
    public void setX(int x) { mx_ = x; }
  }
}

  
  
